package com.zzl.service.impl;

import com.zzl.entiey.User;
import com.zzl.mapper.LoginMapper;
import com.zzl.service.LoginService;
import com.zzl.util.Const;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @ClassName: LoginServiceImpl
 * @Description: GOOD
 * @author: ZZL
 * @date: 2021/7/5  9:49
 */
@Service
public class LoginServiceImpl implements LoginService {

    @Autowired
    LoginMapper loginMapper;

    @Override
    public Boolean register(User user) {
        User userDb = loginMapper.findByUserName(user.getUsername());
            if (userDb==null){
               loginMapper.save(user);
            } else {
                return false;
            }
        return true;
    }

    @Override
    public String login(User user) {
        User userDb = loginMapper.findByUserName(user.getUsername());
        if (!ObjectUtils.isEmpty(userDb)){
             if (userDb.getPassword().equals(user.getPassword())){
                 return Const.LOGIN_SUCCESS;
             }else{
                 return Const.WRONG_PASSWORD;
             }
        }else{
             return Const.NO_USERS_FOUND;
        }
    }

    @Override
    public List<User> findAllUser() {
        return loginMapper.findAllUser();
    }

    @Override
    public void deletedUserByName(String userName) {
        loginMapper.deletedUserByName(userName);
    }
}
